<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book Flask框架 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>蓝图 | Flask框架</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../lan-tu-dan-yuan-ce-shi/dan-yuan-ce-shi.html" />
    
    
    <link rel="prev" href="../lan-tu-dan-yuan-ce-shi.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="4.1"
        data-chapter-title="蓝图"
        data-filepath="lan-tu-dan-yuan-ce-shi/lan-tu.md"
        data-basepath=".."
        data-revision="Fri Aug 24 2018 10:39:47 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Flask框架
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="shi-tu-ji-lu-you.html">
            
                
                    <a href="../shi-tu-ji-lu-you.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        路由和视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="shi-tu-ji-lu-you/flaskjian-jie.html">
            
                
                    <a href="../shi-tu-ji-lu-you/flaskjian-jie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        Flask简介
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="shi-tu-ji-lu-you/flaskhuan-jing-an-zhuang.html">
            
                
                    <a href="../shi-tu-ji-lu-you/flaskhuan-jing-an-zhuang.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        虚拟环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="shi-tu-ji-lu-you/helloworld.html">
            
                
                    <a href="../shi-tu-ji-lu-you/helloworld.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        HelloWorld
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="shi-tu-ji-lu-you/xiang-guan-pei-zhi-can-shu.html">
            
                
                    <a href="../shi-tu-ji-lu-you/xiang-guan-pei-zhi-can-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        相关配置参数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="shi-tu-ji-lu-you/lu-you-de-ge-zhong-ding-yi.html">
            
                
                    <a href="../shi-tu-ji-lu-you/lu-you-de-ge-zhong-ding-yi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        路由信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="shi-tu-ji-lu-you/shi-tu-chang-yong-luo-ji.html">
            
                
                    <a href="../shi-tu-ji-lu-you/shi-tu-chang-yong-luo-ji.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        响应信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="shi-tu-ji-lu-you/zheng-ze-pi-pei-lu-you.html">
            
                
                    <a href="../shi-tu-ji-lu-you/zheng-ze-pi-pei-lu-you.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.7.</b>
                        
                        转换器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="shi-tu-ji-lu-you/yi-chang-bu-huo.html">
            
                
                    <a href="../shi-tu-ji-lu-you/yi-chang-bu-huo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.8.</b>
                        
                        abort,errorhandler
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="shi-tu-ji-lu-you/qing-qiu-gou-zi.html">
            
                
                    <a href="../shi-tu-ji-lu-you/qing-qiu-gou-zi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.9.</b>
                        
                        请求钩子
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="shi-tu-ji-lu-you/huo-qu-qing-qiu-can-shu.html">
            
                
                    <a href="../shi-tu-ji-lu-you/huo-qu-qing-qiu-can-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.10.</b>
                        
                        request请求参数
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="mo-ban.html">
            
                
                    <a href="../mo-ban.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        视图内容和模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="shi-tu-ji-lu-you/zhuang-tai-bao-chi.html">
            
                
                    <a href="../shi-tu-ji-lu-you/zhuang-tai-bao-chi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        状态保持(cookie,session)
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1.1" data-path="shi-tu-ji-lu-you/zhuang-tai-bao-chi/cookie.html">
            
                
                    <a href="../shi-tu-ji-lu-you/zhuang-tai-bao-chi/cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.1.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.1.2" data-path="shi-tu-ji-lu-you/zhuang-tai-bao-chi/session.html">
            
                
                    <a href="../shi-tu-ji-lu-you/zhuang-tai-bao-chi/session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.2.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="shi-tu-ji-lu-you/shang-xia-wen.html">
            
                
                    <a href="../shi-tu-ji-lu-you/shang-xia-wen.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        上下文(理解)
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="shi-tu-ji-lu-you/flask-script.html">
            
                
                    <a href="../shi-tu-ji-lu-you/flask-script.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        flask_script扩展
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="mo-ban/jinja2mo-ban-yin-qing.html">
            
                
                    <a href="../mo-ban/jinja2mo-ban-yin-qing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        jinja2模板概念
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="mo-ban/jinja2mo-ban-yin-qing-shi-yong.html">
            
                
                    <a href="../mo-ban/jinja2mo-ban-yin-qing-shi-yong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        render_template函数,模板语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="mo-ban/guo-lv-qi.html">
            
                
                    <a href="../mo-ban/guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.6.</b>
                        
                        模板过自带滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="mo-ban/zi-ding-yi-guo-lv-qi.html">
            
                
                    <a href="../mo-ban/zi-ding-yi-guo-lv-qi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.7.</b>
                        
                        自定义过滤器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="mo-ban/kong-zhi-dai-ma-kuai.html">
            
                
                    <a href="../mo-ban/kong-zhi-dai-ma-kuai.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.8.</b>
                        
                        模板案例练习
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="mo-ban/mo-ban-dai-ma-fu-yong.html">
            
                
                    <a href="../mo-ban/mo-ban-dai-ma-fu-yong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.</b>
                        
                        模板代码复用
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.9.1" data-path="mo-ban/mo-ban-dai-ma-fu-yong/hong.html">
            
                
                    <a href="../mo-ban/mo-ban-dai-ma-fu-yong/hong.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.1.</b>
                        
                        宏
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9.2" data-path="mo-ban/mo-ban-dai-ma-fu-yong/ji-cheng.html">
            
                
                    <a href="../mo-ban/mo-ban-dai-ma-fu-yong/ji-cheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.2.</b>
                        
                        继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.9.3" data-path="mo-ban/mo-ban-dai-ma-fu-yong/bao-han.html">
            
                
                    <a href="../mo-ban/mo-ban-dai-ma-fu-yong/bao-han.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.9.3.</b>
                        
                        包含
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="mo-ban/te-you-bian-liang-he-han-shu.html">
            
                
                    <a href="../mo-ban/te-you-bian-liang-he-han-shu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.10.</b>
                        
                        模板特有变量
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="mo-ban/csrf.html">
            
                
                    <a href="../mo-ban/csrf.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.11.</b>
                        
                        csrf攻击
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="shu-ju-ku.html">
            
                
                    <a href="../shu-ju-ku.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="shu-ju-ku/orm.html">
            
                
                    <a href="../shu-ju-ku/orm.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        ORM概述
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="shu-ju-ku/flask-sqlalchemyan-zhuang-ji-she-zhi.html">
            
                
                    <a href="../shu-ju-ku/flask-sqlalchemyan-zhuang-ji-she-zhi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        Flask-SQLAlchemy安装及配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="shu-ju-ku/shu-ju-ku-de-ji-ben-cao-zuo.html">
            
                
                    <a href="../shu-ju-ku/shu-ju-ku-de-ji-ben-cao-zuo.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        数据库的基本操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        综合案例-图书管理
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.4.1" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/ding-yi-mo-xing.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.1.</b>
                        
                        数据库配置信息&amp;模型类创建
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4.2" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-ku-biao-chuang-5efa26-ce-shi-shu-ju-tian-jia.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-ku-biao-chuang-5efa26-ce-shi-shu-ju-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.2.</b>
                        
                        数据库表创建&amp;测试数据添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4.3" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shu-ju-xian-793a26-biao-dan-tian-jia.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.3.</b>
                        
                        数据显示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4.4" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/biao-dan-yan-zheng.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.4.</b>
                        
                        数据添加
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.4.5" data-path="shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
            
                
                    <a href="../shu-ju-ku/zong-he-an-4f8b-tu-shu-guan-li/shan-chu-shu-ju.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.5.</b>
                        
                        删除数据
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="shu-ju-ku/duo-dui-duo-yan-lian.html">
            
                
                    <a href="../shu-ju-ku/duo-dui-duo-yan-lian.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        多对多演练
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="shu-ju-ku/shu-ju-ku-qian-yi.html">
            
                
                    <a href="../shu-ju-ku/shu-ju-ku-qian-yi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        数据库迁移
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.7" data-path="shu-ju-ku/chang-jian-guan-xi-mo-ban-dai-ma.html">
            
                
                    <a href="../shu-ju-ku/chang-jian-guan-xi-mo-ban-dai-ma.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.7.</b>
                        
                        常见关系模板代码
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="lan-tu-dan-yuan-ce-shi.html">
            
                
                    <a href="../lan-tu-dan-yuan-ce-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        蓝图&amp;单元测试
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="4.1" data-path="lan-tu-dan-yuan-ce-shi/lan-tu.html">
            
                
                    <a href="../lan-tu-dan-yuan-ce-shi/lan-tu.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        蓝图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="lan-tu-dan-yuan-ce-shi/dan-yuan-ce-shi.html">
            
                
                    <a href="../lan-tu-dan-yuan-ce-shi/dan-yuan-ce-shi.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        单元测试
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >Flask框架</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="blueprint">Blueprint</h1>
<h3 id="&#x6A21;&#x5757;&#x5316;">&#x6A21;&#x5757;&#x5316;</h3>
<p>&#x968F;&#x7740;flask&#x7A0B;&#x5E8F;&#x8D8A;&#x6765;&#x8D8A;&#x590D;&#x6742;,&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5BF9;&#x7A0B;&#x5E8F;&#x8FDB;&#x884C;&#x6A21;&#x5757;&#x5316;&#x7684;&#x5904;&#x7406;,&#x4E4B;&#x524D;&#x5B66;&#x4E60;&#x8FC7;python&#x7684;&#x6A21;&#x5757;&#x5316;&#x7BA1;&#x7406;,&#x4E8E;&#x662F;&#x9488;&#x5BF9;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;flask&#x7A0B;&#x5E8F;&#x8FDB;&#x884C;&#x6A21;&#x5757;&#x5316;&#x5904;&#x7406;</p>
<p>&#x4E3E;&#x4F8B;&#x6765;&#x8BF4;:</p>
<p>&#x6211;&#x4EEC;&#x6709;&#x4E00;&#x4E2A;&#x535A;&#x5BA2;&#x7A0B;&#x5E8F;,&#x524D;&#x53F0;&#x754C;&#x9762;&#x9700;&#x8981;&#x7684;&#x8DEF;&#x7531;&#x4E3A;:&#x9996;&#x9875;,&#x5217;&#x8868;,&#x8BE6;&#x60C5;&#x7B49;&#x9875;&#x9762;</p>
<pre><code class="lang-python">&#x6E90;&#x7A0B;&#x5E8F;app.py&#x6587;&#x4EF6;:
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask

app=Flask(__name__)

<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;index&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/list&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">list</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;list&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/detail&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">detail</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;detail&apos;</span>

<span class="hljs-keyword">if</span> __name__==<span class="hljs-string">&apos;__main__&apos;</span>:
    app.run()
</code></pre>
<p>&#x5982;&#x679C;&#x535A;&#x4E3B;&#x9700;&#x8981;&#x7F16;&#x8F91;&#x535A;&#x5BA2;,&#x8981;&#x8FDB;&#x5165;&#x540E;&#x53F0;&#x8FDB;&#x884C;&#x5904;&#x7406;:&#x540E;&#x53F0;&#x4E3B;&#x9875;,&#x7F16;&#x8F91;,&#x521B;&#x5EFA;,&#x53D1;&#x5E03;&#x535A;&#x5BA2;</p>
<pre><code class="lang-python">&#x6539;&#x8FDB;&#x540E;&#x7A0B;&#x5E8F;:
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask

app=Flask(__name__)

<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;index&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/list&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">list</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;list&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/detail&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">detail</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;detail&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">admin_home</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;admin_home&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/new&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">new</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;new&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/edit&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">edit</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;edit&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/publish&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">publish</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;publish&apos;</span>

<span class="hljs-keyword">if</span> __name__==<span class="hljs-string">&apos;__main__&apos;</span>:
    app.run()
</code></pre>
<ul>
<li>&#x95EE;&#x9898;: &#x8FD9;&#x6837;&#x5C31;&#x4F7F;&#x5F97;&#x6211;&#x4EEC;&#x5728;&#x4E00;&#x4E2A;py&#x6587;&#x4EF6;&#x4E2D;&#x5199;&#x5165;&#x4E86;&#x5F88;&#x591A;&#x8DEF;&#x7531;,&#x5C06;&#x6765;&#x7EF4;&#x62A4;&#x4EE3;&#x7801;&#x4F1A;&#x975E;&#x5E38;&#x9EBB;&#x70E6;,</li>
<li>&#x6B64;&#x65F6;,&#x8003;&#x8651;&#x5230;&#x4E86;&#x6A21;&#x5757;&#x5316;&#x7684;&#x5904;&#x7406;&#x65B9;&#x5F0F;,&#x5C06;admin&#x76F8;&#x5173;&#x7684;&#x8DEF;&#x7531;&#x5199;&#x5230;&#x4E00;&#x4E2A;admin.py&#x6587;&#x4EF6;&#x4E2D;</li>
</ul>
<pre><code class="lang-python">&#x4FEE;&#x6539;&#x540E;&#x7684;&#x4EE3;&#x7801;:
app.py
<span class="hljs-keyword">from</span> flask <span class="hljs-keyword">import</span> Flask

app=Flask(__name__)

<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;index&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/list&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">list</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;list&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/detail&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">detail</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;detail&apos;</span>

<span class="hljs-keyword">if</span> __name__==<span class="hljs-string">&apos;__main__&apos;</span>:
    app.run()

admin.py

<span class="hljs-decorator">@app.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">admin_home</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;admin_home&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/new&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">new</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;new&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/edit&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">edit</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;edit&apos;</span>

<span class="hljs-decorator">@app.route(&apos;/publish&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">publish</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;publish&apos;</span>
</code></pre>
<ul>
<li>&#x95EE;&#x9898;: &#x53D1;&#x73B0;app.py&#x6587;&#x4EF6;&#x4E2D;&#x7684;app&#x76F4;&#x63A5;&#x62A5;&#x9519;,&#x4EE3;&#x7801;&#x65E0;&#x6CD5;&#x7EE7;&#x7EED;&#x5199;&#x4E0B;&#x53BB;</li>
<li>&#x89E3;&#x51B3;: &#x9700;&#x8981;&#x4F7F;&#x7528;&#x84DD;&#x56FE;,flask&#x4E2D;&#x63D0;&#x4F9B;&#x4E86;Blueprint&#x7C7B;,&#x6765;&#x4E13;&#x95E8;&#x5904;&#x7406;&#x6A21;&#x5757;&#x5316;&#x5F00;&#x53D1;</li>
</ul>
<h3 id="blueprint&#x6982;&#x5FF5;">Blueprint&#x6982;&#x5FF5;</h3>
<ul>
<li>&#x7B80;&#x5355;&#x6765;&#x8BF4;&#xFF0C;Blueprint &#x662F;&#x4E00;&#x4E2A;&#x5B58;&#x50A8;&#x64CD;&#x4F5C;&#x65B9;&#x6CD5;&#x7684;&#x5BB9;&#x5668;&#xFF0C;</li>
<li>&#x8FD9;&#x4E9B;&#x64CD;&#x4F5C;&#x5728;&#x8FD9;&#x4E2A;Blueprint &#x88AB;&#x6CE8;&#x518C;&#x5230;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x4E4B;&#x540E;&#x5C31;&#x53EF;&#x4EE5;&#x88AB;&#x8C03;&#x7528;&#xFF0C;</li>
<li>Flask &#x53EF;&#x4EE5;&#x901A;&#x8FC7;Blueprint&#x6765;&#x7EC4;&#x7EC7;URL&#x4EE5;&#x53CA;&#x5904;&#x7406;&#x8BF7;&#x6C42;&#x3002;</li>
</ul>
<p>Flask&#x4F7F;&#x7528;Blueprint&#x8BA9;&#x5E94;&#x7528;&#x5B9E;&#x73B0;&#x6A21;&#x5757;&#x5316;&#xFF0C;&#x5728;Flask&#x4E2D;&#xFF0C;Blueprint&#x5177;&#x6709;&#x5982;&#x4E0B;&#x5C5E;&#x6027;&#xFF1A;</p>
<ul>
<li>&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x53EF;&#x4EE5;&#x5177;&#x6709;&#x591A;&#x4E2A;Blueprint</li>
<li>&#x5728;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x4E2D;&#xFF0C;&#x4E00;&#x4E2A;&#x6A21;&#x5757;&#x53EF;&#x4EE5;&#x6CE8;&#x518C;&#x591A;&#x6B21;</li>
<li>Blueprint&#x53EF;&#x4EE5;&#x5355;&#x72EC;&#x5177;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x6A21;&#x677F;&#x3001;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x6216;&#x8005;&#x5176;&#x5B83;&#x7684;&#x901A;&#x7528;&#x64CD;&#x4F5C;&#x65B9;&#x6CD5;</li>
<li>&#x5728;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x521D;&#x59CB;&#x5316;&#x65F6;&#xFF0C;&#x5C31;&#x5E94;&#x8BE5;&#x8981;&#x6CE8;&#x518C;&#x9700;&#x8981;&#x4F7F;&#x7528;&#x7684;Blueprint</li>
<li>&#x4E00;&#x4E2A;Blueprint&#x5E76;&#x4E0D;&#x662F;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;&#x5E94;&#x7528;&#xFF0C;&#x5B83;&#x4E0D;&#x80FD;&#x72EC;&#x7ACB;&#x4E8E;&#x5E94;&#x7528;&#x8FD0;&#x884C;&#xFF0C;&#x800C;&#x5FC5;&#x987B;&#x8981;&#x6CE8;&#x518C;&#x5230;&#x67D0;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x4E2D;&#x3002;</li>
</ul>
<h3 id="&#x521D;&#x8BC6;&#x84DD;&#x56FE;">&#x521D;&#x8BC6;&#x84DD;&#x56FE;</h3>
<p>&#x84DD;&#x56FE;/Blueprint&#x5BF9;&#x8C61;&#x7528;&#x8D77;&#x6765;&#x548C;&#x4E00;&#x4E2A;&#x5E94;&#x7528;/Flask&#x5BF9;&#x8C61;&#x5DEE;&#x4E0D;&#x591A;&#xFF0C;&#x6700;&#x5927;&#x7684;&#x533A;&#x522B;&#x5728;&#x4E8E;&#x4E00;&#x4E2A; &#x84DD;&#x56FE;&#x5BF9;&#x8C61;&#x6CA1;&#x6709;&#x529E;&#x6CD5;&#x72EC;&#x7ACB;&#x8FD0;&#x884C;&#xFF0C;&#x5FC5;&#x987B;&#x5C06;&#x5B83;&#x6CE8;&#x518C;&#x5230;&#x4E00;&#x4E2A;&#x5E94;&#x7528;&#x5BF9;&#x8C61;&#x4E0A;&#x624D;&#x80FD;&#x751F;&#x6548;</p>
<p>&#x4F7F;&#x7528;&#x84DD;&#x56FE;&#x53EF;&#x4EE5;&#x5206;&#x4E3A;&#x4E09;&#x4E2A;&#x6B65;&#x9AA4;</p>
<ul>
<li>1,&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x84DD;&#x56FE;&#x5BF9;&#x8C61;</li>
</ul>
<pre><code class="lang-python">admin=Blueprint(<span class="hljs-string">&apos;admin&apos;</span>,__name__)
</code></pre>
<ul>
<li>2,&#x5728;&#x8FD9;&#x4E2A;&#x84DD;&#x56FE;&#x5BF9;&#x8C61;&#x4E0A;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;,&#x6CE8;&#x518C;&#x8DEF;&#x7531;,&#x6307;&#x5B9A;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x5939;,&#x6CE8;&#x518C;&#x6A21;&#x7248;&#x8FC7;&#x6EE4;&#x5668;</li>
</ul>
<pre><code class="lang-python"><span class="hljs-decorator">@admin.route(&apos;/&apos;)</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">admin_home</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;admin_home&apos;</span>
</code></pre>
<ul>
<li>3,&#x5728;&#x5E94;&#x7528;&#x5BF9;&#x8C61;&#x4E0A;&#x6CE8;&#x518C;&#x8FD9;&#x4E2A;&#x84DD;&#x56FE;&#x5BF9;&#x8C61;</li>
</ul>
<pre><code class="lang-python">app.register_blueprint(admin,url\_prefix=<span class="hljs-string">&apos;/admin&apos;</span>)
</code></pre>
<p>&#x5F53;&#x8FD9;&#x4E2A;&#x5E94;&#x7528;&#x542F;&#x52A8;&#x540E;,&#x901A;&#x8FC7;/admin/&#x53EF;&#x4EE5;&#x8BBF;&#x95EE;&#x5230;&#x84DD;&#x56FE;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x89C6;&#x56FE;&#x51FD;&#x6570;</p>
<h3 id="&#x84DD;&#x56FE;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x53C2;&#x6570;">&#x84DD;&#x56FE;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x53C2;&#x6570;</h3>
<h4 id="&#x84DD;&#x56FE;&#x7684;url&#x524D;&#x7F00;">&#x84DD;&#x56FE;&#x7684;url&#x524D;&#x7F00;</h4>
<ul>
<li>&#x5F53;&#x6211;&#x4EEC;&#x5728;&#x5E94;&#x7528;&#x5BF9;&#x8C61;&#x4E0A;&#x6CE8;&#x518C;&#x4E00;&#x4E2A;&#x84DD;&#x56FE;&#x65F6;&#xFF0C;&#x53EF;&#x4EE5;&#x6307;&#x5B9A;&#x4E00;&#x4E2A;url_prefix&#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#xFF08;&#x8FD9;&#x4E2A;&#x53C2;&#x6570;&#x9ED8;&#x8BA4;&#x662F;/&#xFF09;</li>
<li><p>&#x5728;&#x5E94;&#x7528;&#x6700;&#x7EC8;&#x7684;&#x8DEF;&#x7531;&#x8868; url_map&#x4E2D;&#xFF0C;&#x5728;&#x84DD;&#x56FE;&#x4E0A;&#x6CE8;&#x518C;&#x7684;&#x8DEF;&#x7531;URL&#x81EA;&#x52A8;&#x88AB;&#x52A0;&#x4E0A;&#x4E86;&#x8FD9;&#x4E2A;&#x524D;&#x7F00;&#xFF0C;&#x8FD9;&#x4E2A;&#x53EF;&#x4EE5;&#x4FDD;&#x8BC1;&#x5728;&#x591A;&#x4E2A;&#x84DD;&#x56FE;&#x4E2D;&#x4F7F;&#x7528;&#x76F8;&#x540C;&#x7684;URL&#x89C4;&#x5219;&#x800C;&#x4E0D;&#x4F1A;&#x6700;&#x7EC8;&#x5F15;&#x8D77;&#x51B2;&#x7A81;&#xFF0C;&#x53EA;&#x8981;&#x5728;&#x6CE8;&#x518C;&#x84DD;&#x56FE;&#x65F6;&#x5C06;&#x4E0D;&#x540C;&#x7684;&#x84DD;&#x56FE;&#x6302;&#x63A5;&#x5230;&#x4E0D;&#x540C;&#x7684;&#x81EA;&#x8DEF;&#x5F84;&#x5373;&#x53EF;</p>
</li>
<li><p>url_for</p>
</li>
</ul>
<pre><code class="lang-python">url_for(<span class="hljs-string">&apos;admin.index&apos;</span>) <span class="hljs-comment"># /admin/</span>
</code></pre>
<h4 id="&#x6CE8;&#x518C;&#x9759;&#x6001;&#x8DEF;&#x7531;">&#x6CE8;&#x518C;&#x9759;&#x6001;&#x8DEF;&#x7531;</h4>
<p>&#x548C;&#x5E94;&#x7528;&#x5BF9;&#x8C61;&#x4E0D;&#x540C;&#xFF0C;&#x84DD;&#x56FE;&#x5BF9;&#x8C61;&#x521B;&#x5EFA;&#x65F6;&#x4E0D;&#x4F1A;&#x9ED8;&#x8BA4;&#x6CE8;&#x518C;&#x9759;&#x6001;&#x76EE;&#x5F55;&#x7684;&#x8DEF;&#x7531;&#x3002;&#x9700;&#x8981;&#x6211;&#x4EEC;&#x5728; &#x521B;&#x5EFA;&#x65F6;&#x6307;&#x5B9A; static_folder &#x53C2;&#x6570;&#x3002;</p>
<p>&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x5C06;&#x84DD;&#x56FE;&#x6240;&#x5728;&#x76EE;&#x5F55;&#x4E0B;&#x7684;static_admin&#x76EE;&#x5F55;&#x8BBE;&#x7F6E;&#x4E3A;&#x9759;&#x6001;&#x76EE;&#x5F55;</p>
<pre><code class="lang-python">admin = Blueprint(<span class="hljs-string">&quot;admin&quot;</span>,__name__,static_folder=<span class="hljs-string">&apos;static_admin&apos;</span>)
app.register_blueprint(admin,url_prefix=<span class="hljs-string">&apos;/admin&apos;</span>)
</code></pre>
<p>&#x73B0;&#x5728;&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;/admin/static_admin/<filename> &#x8BBF;&#x95EE;static_admin&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x9759;&#x6001;&#x6587;&#x4EF6;&#x4E86;
&#x5B9A;&#x5236;&#x9759;&#x6001;&#x76EE;&#x5F55;URL&#x89C4;&#x5219; &#xFF1A;&#x53EF;&#x4EE5;&#x5728;&#x521B;&#x5EFA;&#x84DD;&#x56FE;&#x5BF9;&#x8C61;&#x65F6;&#x4F7F;&#x7528; static_url_path &#x6765;&#x6539;&#x53D8;&#x9759;&#x6001;&#x76EE;&#x5F55;&#x7684;&#x8DEF;&#x7531;&#x3002;&#x4E0B;&#x9762;&#x7684;&#x793A;&#x4F8B;&#x5C06;&#x4E3A; static_admin &#x6587;&#x4EF6;&#x5939;&#x7684;&#x8DEF;&#x7531;&#x8BBE;&#x7F6E;&#x4E3A; /lib</filename></p>
<pre><code class="lang-python">admin = Blueprint(<span class="hljs-string">&quot;admin&quot;</span>,__name__,static_folder=<span class="hljs-string">&apos;static_admin&apos;</span>,static_url_path=<span class="hljs-string">&apos;/lib&apos;</span>)
app.register_blueprint(admin,url_prefix=<span class="hljs-string">&apos;/admin&apos;</span>)
</code></pre>
<h4 id="&#x8BBE;&#x7F6E;&#x6A21;&#x7248;&#x76EE;&#x5F55;">&#x8BBE;&#x7F6E;&#x6A21;&#x7248;&#x76EE;&#x5F55;</h4>
<p>&#x84DD;&#x56FE;&#x5BF9;&#x8C61;&#x9ED8;&#x8BA4;&#x7684;&#x6A21;&#x677F;&#x76EE;&#x5F55;&#x4E3A;&#x7CFB;&#x7EDF;&#x7684;&#x6A21;&#x7248;&#x76EE;&#x5F55;&#xFF0C;&#x53EF;&#x4EE5;&#x5728;&#x521B;&#x5EFA;&#x84DD;&#x56FE;&#x5BF9;&#x8C61;&#x65F6;&#x4F7F;&#x7528; template_folder &#x5173;&#x952E;&#x5B57;&#x53C2;&#x6570;&#x8BBE;&#x7F6E;&#x6A21;&#x677F;&#x76EE;&#x5F55;</p>
<pre><code class="lang-python">admin = Blueprint(<span class="hljs-string">&apos;admin&apos;</span>,__name__,template_folder=<span class="hljs-string">&apos;my_templates&apos;</span>)
</code></pre>
<blockquote>
<p>&#x6CE8;:&#x5982;&#x679C;&#x5728; templates &#x4E2D;&#x5B58;&#x5728;&#x548C; my_templates &#x540C;&#x540D;&#x6587;&#x4EF6;,&#x5219;&#x7CFB;&#x7EDF;&#x4F1A;&#x4F18;&#x5148;&#x4F7F;&#x7528; templates &#x4E2D;&#x7684;&#x6587;&#x4EF6;
&#x53C2;&#x8003;&#x94FE;&#x63A5;&#xFF1A;<a href="https://stackoverflow.com/questions/7974771/flask-blueprint-template-folder" target="_blank">https://stackoverflow.com/questions/7974771/flask-blueprint-template-folder</a></p>
</blockquote>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../lan-tu-dan-yuan-ce-shi.html" class="navigation navigation-prev " aria-label="Previous page: 蓝图&amp;单元测试"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../lan-tu-dan-yuan-ce-shi/dan-yuan-ce-shi.html" class="navigation navigation-next " aria-label="Next page: 单元测试"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-livereload/plugin.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"expandable-chapters-small":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"livereload":{}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book Flask框架 -->
</html>
